package com.ymatou.iapi.prodstock.testcase;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.liveseller.parameter.ActivityProductDto;
import com.ymatou.iapi.liveseller.parameter.CreateLiveActivityBean;
import com.ymatou.iapi.liveseller.parameter.CreateSellerLiveBean;
import com.ymatou.iapi.liveseller.parameter.LiveActivityDto;
import com.ymatou.iapi.prodstock.parameter.DeleteActivityProductByProductIdBean;
import com.ymatou.iapi.prodstock.service.DeleteActivityProductByProductIdCall;
import com.ymatou.iapi.productmanager.parameter.product.AddSellerProductRequest;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.LiveRoomCallService;
import com.ymttest.business.service.ProductManagerCallServiceV2;
import com.ymttest.database.model.LiveRoom;
import com.ymttest.database.model.YmtCatalogsI;
import com.ymttest.database.sqlwapper.LiveRoomWapper;
import com.ymttest.database.sqlwapper.YmtCatalogsIWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.YMTDateUtil;
/**
*通过商品编号删除直播活动商品(直播)
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_DeleteActivityProductByProductId{
	private static DeleteActivityProductByProductIdBean deleteactivityproductbyproductidBean;
	private static DeleteActivityProductByProductIdCall deleteactivityproductbyproductidCall;
	private static int sellerId = Integer.parseInt(EnvSetup.getData("autotest_seller1"));
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("通过商品编号删除直播活动商品(直播)");
	}

	@Before
	public void caseUp() {
		deleteactivityproductbyproductidBean=new DeleteActivityProductByProductIdBean();
		deleteactivityproductbyproductidCall=new DeleteActivityProductByProductIdCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(deleteactivityproductbyproductidCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_DeleteActivityProductByProductId_001() {
		Logger.start(true,"通过商品编号删除直播活动商品");
		try {
			Logger.comment("创建商品");
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			String productId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			Logger.comment("创建直播");
			// 5分钟后开始200分钟后结束
			CreateSellerLiveBean clive = LiveRoomCallService.initCreatesellerliveBean(sellerId);
			Date now = YMTDateUtil.getDate();
			//半小时之内会锁定不能修改
			Date starttime = new Date(now.getTime() + 60 * 60 * 1000);
			Date endtime = new Date(now.getTime() + 200 * 60 * 1000);
			clive.getLiveDto().setStartTime(YMTDateUtil.getFormatTime(starttime, YMTDateUtil.YMDHMS));
			
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(clive);
			Logger.comment("加入商品");
			LiveRoomCallService.AddProductsToLive(createlive.getId(), sellerId, productId);
			Logger.comment("查询商品规格");
			YmtCatalogsIWapper CIWapper = new YmtCatalogsIWapper();
			List<String> cids = CIWapper.selectCatalogsByProductId(productId).stream().map(YmtCatalogsI::getsCatalogId).collect(Collectors.toList());
			Logger.comment("创建秒杀活动");
			//CreateLiveActivityBean createliveactivityBean = LiveRoomCallService.initCreateLiveActivityBean(createlive.getId(), cids.get(0), 6d, 6, starttime);
			
			CreateLiveActivityBean createliveactivityBean = new CreateLiveActivityBean();
			LiveActivityDto LiveActivity=new LiveActivityDto();
			List<ActivityProductDto> ActivityProducts=new ArrayList<>();
			ActivityProductDto activityProductDto1 = new ActivityProductDto();
			activityProductDto1.setProductId(productId);
			activityProductDto1.setActivityPrice(7d);
			activityProductDto1.setActivityStock(6);
			activityProductDto1.setCatalogId(cids.get(0));
			ActivityProductDto activityProductDto2 = new ActivityProductDto();
			activityProductDto2.setProductId(productId);
			activityProductDto2.setActivityPrice(7d);
			activityProductDto2.setActivityStock(6);
			activityProductDto2.setCatalogId(cids.get(1));
			ActivityProducts.add(activityProductDto1);
			ActivityProducts.add(activityProductDto2);
			
			LiveActivity.setActivityProducts(ActivityProducts);
			LiveActivity.setStartTime(String.valueOf(starttime.getTime()));
			LiveActivity.setEndTime(String.valueOf(starttime.getTime()+1*60*1000));
			LiveActivity.setLiveId(createlive.getId());
			LiveActivity.setSellerId(sellerId);
			createliveactivityBean.setLiveActivity(LiveActivity);
			
			createliveactivityBean.getLiveActivity().setActivityProducts(ActivityProducts);
			LiveRoomCallService.CreateLiveActivity(createliveactivityBean);
			LiveRoomWapper LiveRoomWapper = new LiveRoomWapper();
			//直播秒杀活动id
			Integer liveacctivityid = LiveRoomWapper.selectLiveactivityByLiveId(createliveactivityBean.getLiveActivity().getLiveId()).get(0).getLiveActivityId();
			
			deleteactivityproductbyproductidBean.setLiveActivityId(liveacctivityid);
			
			ArrayList<String> list = new ArrayList<String>();
			list.add(productId);
			deleteactivityproductbyproductidBean.setProductIds(list);
			deleteactivityproductbyproductidCall.setData(deleteactivityproductbyproductidBean);
			deleteactivityproductbyproductidCall.callService();		
			
			String Code = deleteactivityproductbyproductidCall.getString("Code");
			Logger.verifyEquals("200", Code, "验证Code");
			Logger.verifyEquals(liveacctivityid.toString(), deleteactivityproductbyproductidCall.getJsonObject().get("Data").getAsJsonObject().get("LiveActivityId").getAsString(), "验证LiveActivityId");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_DeleteActivityProductByProductId_002() {
		Logger.start(true,"通过商品编号删除直播活动商品,多商品");
		try {
			Logger.comment("创建商品");
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			String productId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			Logger.comment("创建直播");
			// 5分钟后开始200分钟后结束
			CreateSellerLiveBean clive = LiveRoomCallService.initCreatesellerliveBean(sellerId);
			Date now = YMTDateUtil.getDate();
			//半小时之内会锁定不能修改
			Date starttime = new Date(now.getTime() + 60 * 60 * 1000);
			Date endtime = new Date(now.getTime() + 200 * 60 * 1000);
			clive.getLiveDto().setStartTime(YMTDateUtil.getFormatTime(starttime, YMTDateUtil.YMDHMS));
			
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(clive);
			Logger.comment("加入商品");
			LiveRoomCallService.AddProductsToLive(createlive.getId(), sellerId, productId);
			Logger.comment("查询商品规格");
			YmtCatalogsIWapper CIWapper = new YmtCatalogsIWapper();
			List<String> cids = CIWapper.selectCatalogsByProductId(productId).stream().map(YmtCatalogsI::getsCatalogId).collect(Collectors.toList());
			Logger.comment("创建秒杀活动");
			//CreateLiveActivityBean createliveactivityBean = LiveRoomCallService.initCreateLiveActivityBean(createlive.getId(), cids.get(0), 6d, 6, starttime);
			
			CreateLiveActivityBean createliveactivityBean = new CreateLiveActivityBean();
			LiveActivityDto LiveActivity=new LiveActivityDto();
			List<ActivityProductDto> ActivityProducts=new ArrayList<>();
			ActivityProductDto activityProductDto1 = new ActivityProductDto();
			activityProductDto1.setProductId(productId);
			activityProductDto1.setActivityPrice(7d);
			activityProductDto1.setActivityStock(6);
			activityProductDto1.setCatalogId(cids.get(0));
			ActivityProductDto activityProductDto2 = new ActivityProductDto();
			activityProductDto2.setProductId(productId);
			activityProductDto2.setActivityPrice(7d);
			activityProductDto2.setActivityStock(6);
			activityProductDto2.setCatalogId(cids.get(1));
			ActivityProducts.add(activityProductDto1);
			ActivityProducts.add(activityProductDto2);
			
			LiveActivity.setActivityProducts(ActivityProducts);
			LiveActivity.setStartTime(String.valueOf(starttime.getTime()));
			LiveActivity.setEndTime(String.valueOf(starttime.getTime()+1*60*1000));
			LiveActivity.setLiveId(createlive.getId());
			LiveActivity.setSellerId(sellerId);
			createliveactivityBean.setLiveActivity(LiveActivity);
			
			createliveactivityBean.getLiveActivity().setActivityProducts(ActivityProducts);
			LiveRoomCallService.CreateLiveActivity(createliveactivityBean);
			LiveRoomWapper LiveRoomWapper = new LiveRoomWapper();
			//直播秒杀活动id
			Integer liveacctivityid = LiveRoomWapper.selectLiveactivityByLiveId(createliveactivityBean.getLiveActivity().getLiveId()).get(0).getLiveActivityId();
			
			deleteactivityproductbyproductidBean.setLiveActivityId(liveacctivityid);
			
			ArrayList<String> list = new ArrayList<String>();
			list.add(productId);
			deleteactivityproductbyproductidBean.setProductIds(list);
			deleteactivityproductbyproductidCall.setData(deleteactivityproductbyproductidBean);
			deleteactivityproductbyproductidCall.callService();		
			
			String Code = deleteactivityproductbyproductidCall.getString("Code");
			Logger.verifyEquals("200", Code, "验证Code");
			Logger.verifyEquals(liveacctivityid.toString(), deleteactivityproductbyproductidCall.getJsonObject().get("Data").getAsJsonObject().get("LiveActivityId").getAsString(), "验证LiveActivityId");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_DeleteActivityProductByProductId_003() {
		Logger.start(true,"通过商品编号删除直播活动商品,多商品,其中一个规格action=-2");
		try {
			Logger.comment("创建商品");
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			addpro.getProduct().getCatalogList().get(0).setAction(-2);
			String productId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			Logger.comment("创建直播");
			// 5分钟后开始200分钟后结束
			CreateSellerLiveBean clive = LiveRoomCallService.initCreatesellerliveBean(sellerId);
			Date now = YMTDateUtil.getDate();
			//半小时之内会锁定不能修改
			Date starttime = new Date(now.getTime() + 60 * 60 * 1000);
			Date endtime = new Date(now.getTime() + 200 * 60 * 1000);
			clive.getLiveDto().setStartTime(YMTDateUtil.getFormatTime(starttime, YMTDateUtil.YMDHMS));
			
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(clive);
			Logger.comment("加入商品");
			LiveRoomCallService.AddProductsToLive(createlive.getId(), sellerId, productId);
			Logger.comment("查询商品规格");
			YmtCatalogsIWapper CIWapper = new YmtCatalogsIWapper();
			List<String> cids = CIWapper.selectCatalogsByProductId(productId).stream().map(YmtCatalogsI::getsCatalogId).collect(Collectors.toList());
			Logger.comment("创建秒杀活动");
			//CreateLiveActivityBean createliveactivityBean = LiveRoomCallService.initCreateLiveActivityBean(createlive.getId(), cids.get(0), 6d, 6, starttime);
			
			CreateLiveActivityBean createliveactivityBean = new CreateLiveActivityBean();
			LiveActivityDto LiveActivity=new LiveActivityDto();
			List<ActivityProductDto> ActivityProducts=new ArrayList<>();
			ActivityProductDto activityProductDto2 = new ActivityProductDto();
			activityProductDto2.setProductId(productId);
			activityProductDto2.setActivityPrice(7d);
			activityProductDto2.setActivityStock(6);
			activityProductDto2.setCatalogId(cids.get(1));
			ActivityProducts.add(activityProductDto2);
			
			LiveActivity.setActivityProducts(ActivityProducts);
			LiveActivity.setStartTime(String.valueOf(starttime.getTime()));
			LiveActivity.setEndTime(String.valueOf(starttime.getTime()+1*60*1000));
			LiveActivity.setLiveId(createlive.getId());
			LiveActivity.setSellerId(sellerId);
			createliveactivityBean.setLiveActivity(LiveActivity);
			
			createliveactivityBean.getLiveActivity().setActivityProducts(ActivityProducts);
			LiveRoomCallService.CreateLiveActivity(createliveactivityBean);
			LiveRoomWapper LiveRoomWapper = new LiveRoomWapper();
			//直播秒杀活动id
			Integer liveacctivityid = LiveRoomWapper.selectLiveactivityByLiveId(createliveactivityBean.getLiveActivity().getLiveId()).get(0).getLiveActivityId();
			
			deleteactivityproductbyproductidBean.setLiveActivityId(liveacctivityid);
			
			ArrayList<String> list = new ArrayList<String>();
			list.add(productId);
			deleteactivityproductbyproductidBean.setProductIds(list);
			deleteactivityproductbyproductidCall.setData(deleteactivityproductbyproductidBean);
			deleteactivityproductbyproductidCall.callService();		
			
			String Code = deleteactivityproductbyproductidCall.getString("Code");
			Logger.verifyEquals("200", Code, "验证Code");
			Logger.verifyEquals(liveacctivityid.toString(), deleteactivityproductbyproductidCall.getJsonObject().get("Data").getAsJsonObject().get("LiveActivityId").getAsString(), "验证LiveActivityId");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}